<?php

namespace Client;
/**
 * Created by PhpStorm.
 * User: 554665488
 * Date: 2018-6-28
 * Time: 19:33
 * @description:
 * @Author: yfl
 * @QQ 554665488
 */

namespace Client;


class RpcClient
{
    private $serviceName;
    private $body;

    //调用不存在的方法时会触发
    public function __call($name, $arg)
    {
        if ($name == 'service') { //意味着会调用
            $this->serviceName = $arg[0];
            return $this;
        }
        //tcp请求发送数据的

        $client = new clientTcpRequest('106.14.120.16', 9503);
        //请求的服务名称,参数
        $data = json_encode([
            'service' => $this->serviceName, //服务名
            'action' => $name, //方法
            'params' => $arg //参数
        ]);

        $res = $client->sync($data);

        //发送http请求
//        $cli = new \Swoole\Http\Client('106.14.120.16', 9501);
//        $cli->set([
//            'Accept'=>'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
//            'Accept-Encoding'=>'gzip, deflate, sdch',
//            'User-Agent'=>'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
//            'Host'=>'www.nizouba.cn',
//            'Connection'=>'keep-alive',
//            'Content-Type'=>'application/x-www-form-urlencoded'
//        ]);
//        $data = [
//            'service' => $this->serviceName, //服务名
//            'action' => $name, //方法
//            'params' => $arg //参数
//        ];
//        $cli->post('/',$data,function ($cli){
//
////            echo "Length: " . strlen($cli->body) . "\n";
//           $this->body=$cli->body;
//           var_dump($this->body);
////            return $cli->body;
//        });

        //初始化
//        $curl = curl_init();
//        //设置抓取的url
//        curl_setopt($curl, CURLOPT_URL, 'http://106.14.120.16:9501');
//        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36');
//        //设置头文件的信息作为数据流输出
//        curl_setopt($curl, CURLOPT_HEADER, 1);
//        //设置获取的信息以文件流的形式返回，而不是直接输出。
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        //设置post方式提交
//        curl_setopt($curl, CURLOPT_POST, 1);
//        $returnCookie = 0;
//        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
//        //设置超时时间
//        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
//        curl_setopt($curl, CURLOPT_COOKIE, 'aaa');
//        //设置post数据
//        $post_data = array(
//            'service' => $this->serviceName, //服务名
//            'action' => $name, //方法
//            'params' => $arg //参数
//        );
//        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
//        //执行命令
//        $data = curl_exec($curl);
//        if (curl_error($curl)) {
//            return curl_error($curl);
//        }
////        var_dump($returnCookie);
//        if ($returnCookie) {
//            list($header, $body) = explode('\r\n\r\n', $data, 2);
//            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
//            $info['cookie'] = substr($matches[1][0], 1);
//            $info['content'] = $body;
//        }
////        if (isset($info)) var_dump($info);
//        //关闭URL请求
//        curl_close($curl);
//        //显示获得的数据
////        print_r($data);

        return $res;
    }
}